package com.isdepci.project.factor.service.impl;

import java.util.List;

import com.isdepci.common.utils.StringUtils;
import com.isdepci.framework.web.domain.AjaxResult;
import com.isdepci.project.factor.utils.FactorRedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.isdepci.project.factor.mapper.EnterpriseInfoMapper;
import com.isdepci.project.factor.domain.EnterpriseInfo;
import com.isdepci.project.factor.service.IEnterpriseInfoService;

/**
 * 企业库Service业务层处理
 *
 * @author isdepci
 * @date 2024-11-07
 */
@Service
public class EnterpriseInfoServiceImpl implements IEnterpriseInfoService {
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;

 
    /**
     * 查询企业库
     */
    @Override
    public EnterpriseInfo selectEnterpriseInfoById(Long id) {
        return enterpriseInfoMapper.selectEnterpriseInfoById(id);
    }

    /**
     * 查询企业库列表
     */
    @Override
    public List<EnterpriseInfo> selectEnterpriseInfoList(EnterpriseInfo enterpriseInfo) {
        return enterpriseInfoMapper.selectEnterpriseInfoList(enterpriseInfo);
    }

    /**
     * 新增企业库
     */
    @Override
    public AjaxResult insertEnterpriseInfo(EnterpriseInfo enterpriseInfo) {
        if(StringUtils.isEmpty(enterpriseInfo.getEnterpriseName())){
            return AjaxResult.error("名称不能为空！");
        }
        List<EnterpriseInfo> oldList = enterpriseInfoMapper.selectEnterpriseInfoByName(enterpriseInfo.getEnterpriseName());
        if(oldList.size()>0){
            return AjaxResult.error("当前名称已录入！");
        }
        if(enterpriseInfo.getParentId()!=0){
            EnterpriseInfo info = enterpriseInfoMapper.selectEnterpriseInfoById(enterpriseInfo.getParentId());
            if (info.getEnterpriseStatus() != 1) {
                return AjaxResult.error("当前分类停用，不允许新增");
            }
            enterpriseInfo.setAncestors(info.getAncestors() + "," + enterpriseInfo.getParentId());
        }else {
            enterpriseInfo.setAncestors("0");
        }
        enterpriseInfo.setCreateUser();
        int row = enterpriseInfoMapper.insertEnterpriseInfo(enterpriseInfo);
        if (row > 0) {
            FactorRedisUtils.clearEnterpriseInfoListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改企业库
     */
    @Override
    public AjaxResult updateEnterpriseInfo(EnterpriseInfo enterpriseInfo) {
        EnterpriseInfo old = enterpriseInfoMapper.selectEnterpriseInfoById(enterpriseInfo.getId());
        if(StringUtils.isNotEmpty(enterpriseInfo.getEnterpriseName())&& !old.getEnterpriseName().equals(enterpriseInfo.getEnterpriseName())){
            List<EnterpriseInfo> oldList = enterpriseInfoMapper.selectEnterpriseInfoByName(enterpriseInfo.getEnterpriseName());
            if(oldList.size()>0){
                return AjaxResult.error("当前名称已录入！");
            }
        }
        EnterpriseInfo newParent = enterpriseInfoMapper.selectEnterpriseInfoById(enterpriseInfo.getParentId());
        if (StringUtils.isNotNull(newParent) && StringUtils.isNotNull(old)) {
            String newAncestors = newParent.getAncestors() + "," + newParent.getId();
            String oldAncestors = old.getAncestors();
            enterpriseInfo.setAncestors(newAncestors);
            updateChildren(enterpriseInfo.getId(), newAncestors, oldAncestors);
        }
        enterpriseInfo.setUpdateUser();
        int row = enterpriseInfoMapper.updateEnterpriseInfo(enterpriseInfo);
        if (enterpriseInfo.getEnterpriseStatus() == 1) {
            updateParentStatus(enterpriseInfo);
        }
        if(old.getEnterpriseStatus()==1&&enterpriseInfo.getEnterpriseStatus()!=1){
            enterpriseInfoMapper.updateChildrenStatus(enterpriseInfo.getId(),enterpriseInfo.getEnterpriseStatus());
        }
        if (row > 0) {
            FactorRedisUtils.clearEnterpriseInfoListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    public void updateChildren(Long id, String newAncestors, String oldAncestors) {
        List<EnterpriseInfo> children = enterpriseInfoMapper.selectChildrenById(id);
        for (EnterpriseInfo child : children) {
            child.setAncestors(child.getAncestors().replace(oldAncestors, newAncestors));
        }
        if (children.size() > 0) {
            enterpriseInfoMapper.updateChildren(children);
        }
    }

    private void updateParentStatus(EnterpriseInfo info){
        String updateBy = info.getUpdateBy();
        info = enterpriseInfoMapper.selectEnterpriseInfoById(info.getId());
        info.setUpdateBy(updateBy);
        enterpriseInfoMapper.updateParentStatus(info);
    }

    /**
     * 删除企业库信息
     */
    @Override
    public AjaxResult deleteEnterpriseInfoById(Long id) {
        List<EnterpriseInfo> children = enterpriseInfoMapper.selectChildrenById(id);
        if (children.size()>0) {
            return AjaxResult.error("存在下级分类,不允许删除");
        }
        int row = enterpriseInfoMapper.deleteEnterpriseInfoById(id);
        if (row > 0) {
            FactorRedisUtils.clearEnterpriseInfoListCache();
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
